<?php
/*
 * 
 * 
 * 
 */
class uploads{
		
	public $base_path = 'upload/';
	function __construct(){
		//$this->base_path = ROOTPATH.'upload/';
	}
	
	public function upload_file($field,$dir=NULL){	
		if(!isset($_FILES[$field])){
			echo  'not found upload file';	
			return FALSE;
		}
		if($dir != NULL){
				$this->base_path .= $dir."/";
				if(!is_dir($this->base_path)){
					echo $dir.'no found';	
					return FALSE;
				}
		}
		$pic = array();
		if(isset($_FILES[$field]['name'][0])){
			foreach($_FILES[$field]['name'] as $key=>$val){
				$name = $_FILES[$field]['name'][$key];
				$file_name = $this->base_path.md5($name.time()).'.'.$this->file_ext($name);
					
				$pic[$key]['file_name'] = $file_name;
				$pic[$key]['file_ext'] = $this->file_ext($name);
					
				if(move_uploaded_file($_FILES[$field]['tmp_name'][$key], ROOTPATH.$file_name) == FALSE){
					echo 'upload error';
					return FALSE;
			    }				
			}
			return $pic;
		}
		return FALSE;
	}
	
	private function file_ext($file_name){
		$file = strrev($file_name);
		$pos = stripos($file,'.');
		if($pos === FALSE){
			return FALSE;
		}
		$ext = substr($file,0,$pos);
		return strrev($ext);
	}
} 
?>